package com.pkk.penguin.frame.know.java.Javacoreknowledge.threadparallel;

import com.pkk.penguin.frame.know.java.Javacoreknowledge.thread.ThreadPoolSingle;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import org.junit.Test;

/**
 * @description: 多线程并发执行
 * @author: peikunkun
 * @create: 2020-01-20 09:55
 **/
public class ThreadparallelTests {


  @Test
  public void Administrator_21_20200120110051() {
    System.out.println("欢迎使用单元测试方法【Administrator_21()_20200120110051】");
    System.out.println("此方法测试描述：【】");
    final ThreadPoolExecutor instand = ThreadPoolSingle.instand();
    final long beginTime = System.currentTimeMillis();
    List<CompletableFuture<String>> futures = new ArrayList<>();
    futures.add(CompletableFuture.supplyAsync(this::method, instand));
    futures.add(CompletableFuture.supplyAsync(this::method, instand));
    futures.add(CompletableFuture.supplyAsync(this::method, instand));
    futures.add(CompletableFuture.supplyAsync(this::method, instand));

    final CompletableFuture<Void> voidCompletableFuture = CompletableFuture
        .allOf(futures.toArray(new CompletableFuture[4]));

    final CompletableFuture<List<String>> listCompletableFuture = voidCompletableFuture
        .thenApplyAsync(value -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList()),
            instand
        );
    final List<String> join = listCompletableFuture.join();
    System.out.println("业务执行完成,耗时:" + (System.currentTimeMillis() - beginTime) + ",返回值:" + join);
  }


  @Test
  public void Administrator_13_20200120095913() {
    System.out.println("欢迎使用单元测试方法【Administrator_13()_20200120095913】");
    System.out.println("此方法测试描述：【线程池】");
    final ThreadPoolExecutor instand = ThreadPoolSingle.instand();
    final long beginTime = System.currentTimeMillis();
    final ArrayList<Future<String>> futures = new ArrayList<>();
    futures.add(instand.submit(this::method));
    futures.add(instand.submit(this::method));
    futures.add(instand.submit(this::method));
    futures.add(instand.submit(this::method));
    final List<String> collect = futures.stream().map(future -> {
      try {
        return future.get();
      } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
      } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
      }
    }).collect(Collectors.toList());

    System.out.println("业务执行完成,耗时:" + (System.currentTimeMillis() - beginTime) + ",返回值:" + collect);
  }


  @Test
  public void Administrator_11_20200120095610() {
    System.out.println("欢迎使用单元测试方法【Administrator_11()_20200120095610】");
    System.out.println("此方法测试描述：【普通方法执行】");
    final long beginTime = System.currentTimeMillis();
    //业务逻辑1
    method();
    //业务逻辑2
    method();
    //业务逻辑3
    method();
    //业务逻辑4
    method();
    System.out.println("业务执行完成,耗时:" + (System.currentTimeMillis() - beginTime));
  }


  private String method() {
    try {
      System.out.println(Thread.currentThread().getName() + " is Running");
      Thread.sleep(1000L);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return Thread.currentThread().getName();
  }

}
